package cn.bdqn.controller;

import cn.bdqn.domain.SendEquip;
import cn.bdqn.service.SendEquipService;
import cn.bdqn.service.SendFreightService;
import cn.bdqn.utils.result.Result;
import cn.bdqn.utils.result.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 发货单业务控制层
 * @author dkj
 */
@RestController
public class SendEquipController {
    /**
     * 创建日志记录器
     */
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SendEquipService sendEquipService;



    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/sendEquip/query/{id}")
    public Result queryByPrimaryKey(@PathVariable("id") Integer id){

        try {
            logger.info("进入了方法：参数：id={}",id);
            //根据id查询
            SendEquip sendEquip = sendEquipService.queryByPrimaryKey(id);
            //返回结果
            return ResultGenerator.genSuccessResult(sendEquip);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 根据发货单id查询
     * @param sendFreightId
     * @return
     */
    @GetMapping("/sendEquip/querys/{sendFreightId}")
    public Result queryBySendFreightId(@PathVariable("sendFreightId") Integer sendFreightId){

        try {
            logger.info("进入了方法，参数：sendFreightId={}",sendFreightId);
            //根据发货id查询
            List<SendEquip> sendEquipList = sendEquipService.queryBySendFreightId(sendFreightId);
            //返回结果
            return ResultGenerator.genSuccessResult(sendEquipList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("查询失败");
        }
    }


    /**
     * 新增
     * @param sendEquips
     * @return
     */
    @PostMapping("/sendEquip/save/")
    public Result saveSendEquip(@RequestBody List<SendEquip> sendEquips){

        try {
            logger.info("进入方法，参数：sendEquips={}",sendEquips);
            //添加
            List<SendEquip> sendEquipList = sendEquipService.saveSendEquip(sendEquips);
            //返回结果
            return ResultGenerator.genSuccessResult(sendEquipList);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("添加失败");
        }

    }


    /**
     * 更新
     * @param sendEquip
     */
    @PutMapping("/sendEquip/update/")
    public Result updateSendEquip(@RequestBody SendEquip sendEquip){

        try {
            logger.info("进入方法了，参数：sendEquip={}",sendEquip);
            sendEquipService.updateSendEquip(sendEquip);
            //返回结果
            return ResultGenerator.genSuccessResult();
        }catch (Exception e){
            e.printStackTrace();
            logger.error("报错了："+e.getMessage());
            return ResultGenerator.genFailResult("修改失败");
        }
    }

}
